<?php
/**
 * @created on 19.5.2006
 * 
 */
 
class cView
{
	public $cms;
	
	/** xhtml/xml/rss2/rss/atom/raw */
	public $doctype;
	
	/** cs/en/de.. */
	public $lang;
	
	/** short/full */
	public $length;

	/** show,edit,insert */
	public $action;

	/** frontend/backend */
	public $mode;
	
	public $id;
		

	/** Nový view, $params jsou action[show/edit/insert] doctype[xhtml/raw/rss..] mode[frontend/backend] length[full/short] */
	function __construct($params, cView $defaultView=null, cCMS &$cms=null)
	{
		if ($cms!=null) { 
			$this->cms=$cms; 
		} else if ($defaultView->cms) {
			$this->cms = $defaultView->cms;
		}
		
		if (!is_array($params)) throw new cException("Parametry musí být polem!");
		if ($defaultView!=null) {
			$params = array_merge($defaultView->getParams(), $params);
		} 
		$this->override($params);
		$this->domPath = new cPath(); 		
	}
	
	function override($params) {
		foreach ($params as $k=>$v) 
		{
			if ($v!='' || $this->cms->defaultView->$k=='') 
			{
				$this->{$k}=strToLower($v);
			}
		}
	}
	
	function recognize() 
	{
		$uri = $_SERVER["REQUEST_URI"];
		
		if ($_GET["__path"]!=null) 
		{
			$path = $_GET["__path"];
			unset($_GET["__path"]);	
		} else if (($uri = strstr($uri,"?"))!="" && strlen(strstr($uri,"&")>=strlen(strstr($uri,"=")))) {
			$path = substr($uri,1,strlen($uri)-strlen(strstr($uri,"&")));
		}
		
		if (strlen($path)>0) 
		{
			$nodes = array_reverse(explode("/",$path));
			foreach ($nodes as $n) 
			{
				$row = $this->cms->tree->getRaw("standard_treeitem.string_id='".mysql_real_escape_string($n)."'");
				if (($id = $row[0]["id"])!="") 
				{
					$this->id = $id;
					break;
				}	
			}
		}
		$this->override($_GET);
		if (!isset($_POST['keepget'])) {
			$this->override($_POST);
		}
	}
	
	function getUrl($override=null) 
	{
		
		$attrs = $this->getParams();
		if ($override!=null) {
			$attrs = array_merge($attrs, $override);
		}
		//print_r($attrs);
		
		if ($attrs["mode"]=="backend") 
		{
			$s=array();
			foreach ($attrs as $k=>$v) {
				if ($this->cms->defaultView->{$k}!=$v && isset($override[$k])) {
					$s[$k]=$k."=".$v;
				}
			}

			return "?".implode("&amp;",$s);
		} else {
			foreach ($attrs as $k=>$v) {
				if ($this->cms->defaultView->{$k}!=$v && isset($override[$k])) {
					$s[$k]=$k."=".$v;
				}
			}
			if (1) //count($s)==1 && isset($s["id"])) 
			{
				//udělat fancy adresu
				$row = $this->cms->tree->getRaw("id=".$attrs['id']);
				if (count($row)==0) throw new cException("ajéje, nenašel jsem nikoho, kdo by odpovídal id = ".$attrs['id']);
				$path = $this->cms->tree->getRaw("appears_in_path=1 AND lft<=".$row[0]["lft"]." AND rgt>=".$row[0]["rgt"]." ORDER BY lft");
				$p = array();
				foreach ($path as $n) 
				{
					//ze začátku se párkrát stalo, že se insertnul špatný node, pro jistotu
					if ($n["string_id"]!="") {
						$p[]= $n["string_id"];
					}
				}			
				
				if (is_array($p)) { $destUrl = implode("/",$p); }

				unset($s["id"]);
				if (count($s)>0) {
					$tail = "?".implode("&amp;",$s);
				}
				
				// chcete tz linky palive, nebo jemne?
				if ($this->{'@links'}!='absolute') 
				{
					$url = $this->cms->file->getRelative($this->cms->url,$destUrl).$tail;
				} else $url = $this->cms->siteUrl.$destUrl.'/'.$tail;
				
				
				if ($url=="") $url = "./";
				return $url;
			}			
			if (is_array($s)) { return "?".implode("&amp;",$s); } else return;
		}
	}
	
	function getParams() {
		$attrs = get_object_vars($this);
		unset($attrs["domPath"]);
		unset($attrs["cms"]);
		return $attrs; 
	}
	
	function setLinks($kind) 
	{
		$this->{'@links'} = $kind;
	}
	
}

class cPath {
	
	function add($name) {
		$this->path[]=$name;
	}
	
	function get() {
		return $this->path;
	}
	
	function clear() {
		$this->path=array();
	}
	function __construct() {
		$this->clear();
	}
	function shorten() {
		array_pop($this->path);
	}
}  

 
?>